import * as knex from 'knex';
import {IMysqlConfig} from './config';
import Query from "./sql/Query";
import Insert from './sql/Insert';
import Truncate from './sql/Truncate';
import Update, {IUpdateData} from "./sql/Update";
import Delete from "./sql/Delete";
import MyCrypto from "../MyCrypto";

export interface ICreateConnectionProps {
    DBConfig:IMysqlConfig
}

export default class DBConnection{
    private readonly _knex;
    private readonly _Query:Query;
    private readonly _Insert:Insert;
    private readonly _Truncate:Truncate;
    private readonly _Update:Update;
    private readonly _Delete:Delete;
    private readonly _crypto = new MyCrypto();

    constructor(props:ICreateConnectionProps){
        const {DBConfig} = props;
        this._knex =  knex({client: 'mysql',connection:DBConfig});

        this._Query = new Query({knex:this._knex});
        this._Insert = new Insert({knex:this._knex});
        this._Truncate = new Truncate({knex:this._knex});
        this._Update = new Update({knex:this._knex});
        this._Delete = new Delete({knex:this._knex});
    };

    get knex() {
        return this._knex;
    }

    get Query():Query {
        return this._Query;
    }

    get Insert():Insert {
        return this._Insert;
    }

    get Truncate():Truncate {
        return this._Truncate;
    }

    get Update():Update {
        return this._Update;
    }

    get Delete(): Delete {
        return this._Delete;
    }

    encode(DATA:IUpdateData){
        return this._crypto.dataEncode(DATA);
    }

    strEncode(DATA:string):string{
        return this._crypto.strEncode(DATA);
    }


    strDecode(DATA:string):string{
        return this._crypto.strDecode(DATA);
    }
}